/*
* Copyright 2007 University of Oxford
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function codegoo_settingsPopulate() {
  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  var defaultSearchPage;

  if (prefs.getPrefType("extensions.defaultSearchPage") == prefs.PREF_STRING) {
    defaultSearchPage = prefs.getCharPref("extensions.defaultSearchPage");
  } else {
    prefs.setCharPref("extensions.defaultSearchPage", "wiki");
    defaultSearchPage = prefs.getCharPref("extensions.defaultSearchPage");
  }
  
  var searchPage = document.getElementById('defaultSearchPage');
  if (searchPage != null) {
    searchPage.value = defaultSearchPage;
  }
  
  var issueSortMilestone;
  if (prefs.getPrefType("extensions.issueSortMilestone") == prefs.PREF_BOOL) {
    issueSortMilestone = prefs.getBoolPref("extensions.issueSortMilestone");
  } else {
    prefs.setBoolPref("extensions.issueSortMilestone", true);
    issueSortMilestone = prefs.getBoolPref("extensions.issueSortMilestone");
  }
  
  var issueSortMilestoneCheck = document.getElementById('issueSortMilestone');
  if (issueSortMilestoneCheck != null) {
    issueSortMilestoneCheck.checked = issueSortMilestone;
  }
  
  var issueSortPriority;
  if (prefs.getPrefType("extensions.issueSortPriority") == prefs.PREF_BOOL) {
    issueSortPriority = prefs.getBoolPref("extensions.issueSortPriority");
  } else {
    prefs.setBoolPref("extensions.issueSortPriority", true);
    issueSortPriority = prefs.getBoolPref("extensions.issueSortPriority");
  }
  
  var issueSortPriorityCheck = document.getElementById('issueSortPriority');
  if (issueSortPriorityCheck != null) {
    issueSortPriorityCheck.checked = issueSortPriority;
  }
  
  var issueSortOwner;
  if (prefs.getPrefType("extensions.issueSortOwner") == prefs.PREF_BOOL) {
    issueSortOwner = prefs.getBoolPref("extensions.issueSortOwner");
  } else {
    prefs.setBoolPref("extensions.issueSortOwner", false);
    issueSortOwner = prefs.getBoolPref("extensions.issueSortOwner");
  }
  
  var issueSortOwnerCheck = document.getElementById('issueSortOwner');
  if (issueSortOwnerCheck != null) {
    issueSortOwnerCheck.checked = issueSortOwner;
  }
  
  var issueSortType;
  if (prefs.getPrefType("extensions.issueSortType") == prefs.PREF_BOOL) {
    issueSortType = prefs.getBoolPref("extensions.issueSortType");
  } else {
    prefs.setBoolPref("extensions.issueSortType", false);
    issueSortType = prefs.getBoolPref("extensions.issueSortType");
  }
  
  var issueSortTypeCheck = document.getElementById('issueSortType');
  if (issueSortTypeCheck != null) {
    issueSortTypeCheck.checked = issueSortType;
  }
  
  var issueSortStatus;
  if (prefs.getPrefType("extensions.issueSortStatus") == prefs.PREF_BOOL) {
    issueSortStatus = prefs.getBoolPref("extensions.issueSortStatus");
  } else {
    prefs.setBoolPref("extensions.issueSortStatus", true);
    issueSortStatus = prefs.getBoolPref("extensions.issueSortStatus");
  }
  
  var issueSortStatusCheck = document.getElementById('issueSortStatus');
  if (issueSortStatusCheck != null) {
    issueSortStatusCheck.checked = issueSortStatus;
  }
  
  // Popular issues rating
  var popularIssuesRating;;
  if (prefs.getPrefType("extensions.popularIssuesRating") == prefs.PREF_INT) {
    popularIssuesRating = prefs.getIntPref("extensions.popularIssuesRating");
  } else {
    prefs.setIntPref("extensions.popularIssuesRating", 5);
    popularIssuesRating = prefs.getBoolPref("extensions.popularIssuesRating");
  }
  
  var popularIssuesRatingTextbox = document.getElementById('popularIssuesRating');
  if (popularIssuesRatingTextbox != null) {
    popularIssuesRatingTextbox.value = popularIssuesRating;
  }
}

function codegoo_settingsOK() {
  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  
  var defaultSearchPage = document.getElementById('defaultSearchPage').selectedItem;
  if (defaultSearchPage != null) {
    prefs.setCharPref("extensions.defaultSearchPage", defaultSearchPage.value);
  }

  var issueSortMilestone = document.getElementById('issueSortMilestone');
  if (issueSortMilestone != null) {
    prefs.setBoolPref("extensions.issueSortMilestone", issueSortMilestone.checked);
  }

  var issueSortPriority = document.getElementById('issueSortPriority');
  if (issueSortPriority != null) {
    prefs.setBoolPref("extensions.issueSortPriority", issueSortPriority.checked);
  }

  var issueSortOwner = document.getElementById('issueSortOwner');
  if (issueSortOwner != null) {
    prefs.setBoolPref("extensions.issueSortOwner", issueSortOwner.checked);
  }

  var issueSortType = document.getElementById('issueSortType');
  if (issueSortType != null) {
    prefs.setBoolPref("extensions.issueSortType", issueSortType.checked);
  }

  var issueSortStatus = document.getElementById('issueSortStatus');
  if (issueSortStatus != null) {
    prefs.setBoolPref("extensions.issueSortStatus", issueSortStatus.checked);
  }
  
  // Popular Issues Rating
  var popularIssuesRating = document.getElementById('popularIssuesRating');
  if (popularIssuesRating != null) {
    prefs.setIntPref("extensions.popularIssuesRating", popularIssuesRating.value);
  }  
  
  return true;
}

function codegoo_settingsCancel() {
  // do nothing
  return true;
}

